import { Injectable } from '@nestjs/common';
import { SysDictDataRepository } from './repositories/sys-dict-data.repository';
import { SysDictData } from './entities/sys-dict-data.entity';
import { DictUtils } from '~/ruoyi-share/utils/dict.utils';

@Injectable()
export class SysDictDataService {
  constructor(
    private readonly dictDataRepository: SysDictDataRepository,
    private readonly dictUtils: DictUtils 
    ) {}

  /**
   * 根据条件分页查询字典数据
   * 
   * @param dictData 字典数据信息
   * @return 字典数据集合信息
   */
  async selectDictDataList(dictData: SysDictData): Promise<[SysDictData[], number]> {
    return this.dictDataRepository.selectDictDataList(dictData);
  }

  /**
   * 根据字典类型和字典键值查询字典数据信息
   * 
   * @param dictType 字典类型
   * @param dictValue 字典键值
   * @return 字典标签
   */
  async selectDictLabel(dictType: string, dictValue: string): Promise<string> {
    return this.dictDataRepository.selectDictLabel(dictType, dictValue);
  }

  /**
   * 根据字典数据ID查询信息
   * 
   * @param dictCode 字典数据ID
   * @return 字典数据
   */
  async selectDictDataById(dictCode: number): Promise<SysDictData> {
    return this.dictDataRepository.selectDictDataById(dictCode);
  }

  /**
   * 批量删除字典数据信息
   * 
   * @param dictCodes 需要删除的字典数据ID
   */
  async deleteDictDataByIds(dictCodes: number[]): Promise<void> {
    for (const dictCode of dictCodes) {
      const data = await this.selectDictDataById(dictCode);
      await this.dictDataRepository.deleteDictDataById(dictCode);
      const dictDatas = await this.dictDataRepository.selectDictDataByType(data.dictType);
      // Assuming DictUtils.setDictCache is a utility function to update cache
      // DictUtils.setDictCache(data.dictType, dictDatas);
    }
  }

  /**
   * 新增保存字典数据信息
   * 
   * @param data 字典数据信息
   * @return 结果
   */
  async insertDictData(data: SysDictData): Promise<void> {
    await this.dictDataRepository.insertDictData(data);
    const dictDatas = await this.dictDataRepository.selectDictDataByType(data.dictType);
    // Assuming DictUtils.setDictCache is a utility function to update cache
    // DictUtils.setDictCache(data.dictType, dictDatas);
  }

  /**
   * 修改保存字典数据信息
   * 
   * @param data 字典数据信息
   * @return 结果
   */
  async updateDictData(data: SysDictData): Promise<number> {
    const row = await this.dictDataRepository.updateDictData(data);
    if (row > 0) {
      const dictDatas = await this.dictDataRepository.selectDictDataByType(data.dictType);
      this.dictUtils.setDictCache(data.dictType, dictDatas);
    }
    return row;
  }
}
